<template>
  <div class="container">
    <el-form>
       <el-form-item label="标题">
        <el-input v-model="form.title" placeholder="请输入标题"></el-input>
        <el-switch
          class="switch"
          v-model="form.show"
          active-color="#ff4949"
          inactive-color="#dcdfe6">
        </el-switch>
      </el-form-item>
      
      <el-form-item label="关联商品">
        <el-radio-group v-model="form.product_page">
          <el-radio v-for="(item, index) in list" :key="index" :label="item.id.toString()">{{item.title}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-button type="primary" @click="save" >保存</el-button>
    </el-form>
  </div>
</template>

<script>
import { setSelfRecommend, getSelfRecommend} from '@/api/others'
import { getPageList } from '@/api/autarky'
export default {
  data() {
    return {
      form: {
        title: '',
        product_page: '',
        show: false
      },
      list: []
    }
  },
  mounted() {
    this.getPage()
    this.getSelfRecommend()
  },
  methods: {
    getPage() {
      getPageList().then(res => {
        if (res.code === 10000) {
          this.list = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    getSelfRecommend() {
      getSelfRecommend().then(res => {
        if (res.code === 10000) {
          res.data.show = res.data.show === 1
          this.form = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    save() {
      console.log(this.form)
      if (this.form.title === '') {
        this.$message.error('请输入标题')
        return
      }
      if (this.form.product_page === '') {
        this.$message.error('请选择专场')
        return
      }
      const params = {
        ...this.form
      }
      params.show = params.show ? '1' : '0'
      setSelfRecommend(params).then(res => {
        console.log(res)
        if (res.code === 10000) {
          this.$message.success('保存成功')
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="scss">
  .container {
    padding: 20px;
    ::v-deep .el-input {
      width: 300px;
    }
    .switch {
      margin-left: 300px;
    }
  }
</style>